library(ggplot2)
part07_wrurace<- read.csv("part07_wrurace.csv")
part09_wrurace<- read.csv("part09_wrurace.csv")
part11_wrurace<- read.csv("part11_wrurace.csv")
part13_wrurace<- read.csv("part13_wrurace.csv")
times100<- function(df){
  df$Mean<- df$Mean*100
  df$ci <- df$ci*100
  return(df)
}
part07_wrurace<- times100(part07_wrurace)
part09_wrurace<- times100(part09_wrurace)
part11_wrurace<- times100(part11_wrurace)
part13_wrurace<- times100(part13_wrurace)


#2013 vs 2017
df0711<- bind_rows(part07_wrurace, part11_wrurace)
ggr0711_2<- ggplot(df0711, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2007 to 2011")+xlab("Ethnorace")+ylab("Rate of participation")
df0711<- bind_cols(part07_wrurace, part11_wrurace)
df0711$diff<- df0711$Mean...7- df0711$Mean...2
df0711$diffci<- 1.96*100*sqrt( ((df0711$Mean...7/100)*(1-df0711$Mean...7/100)/df0711$Freq...8) + 
                             ((df0711$Mean...2/100)*(1-df0711$Mean...2/100)/df0711$Freq...3))
ggr0711_1<- ggplot(df0711, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")



#2015 vs 2019
df0913<- bind_rows(part09_wrurace, part13_wrurace)
ggr0913_2<- ggplot(df0913, aes(x=wrurace, y=Mean, colour=as.factor(yr))) + 
  geom_errorbar(aes(ymin=Mean-ci, ymax=Mean+ci), width=1) +
  geom_line() +
  geom_point()+ ggtitle("2009 to 2013")+xlab("Ethnorace")+ylab("Rate of participation")
df0913<- bind_cols(part09_wrurace, part13_wrurace)
df0913$diff<- df0913$Mean...7- df0913$Mean...2
df0913$diffci<- 1.96*100*sqrt( ((df0913$Mean...7/100)*(1-df0913$Mean...7/100)/df0913$Freq...8) + 
                             ((df0913$Mean...2/100)*(1-df0913$Mean...2/100)/df0913$Freq...3))
ggr0913_1<- ggplot(df0913, aes(x=wrurace...1, y=diff)) + 
  geom_errorbar(aes(ymin=diff-diffci, ymax=diff+diffci), width=1) +
  geom_line() +
  geom_point()+geom_hline(yintercept=0, color="red")+ xlab("Ethnorace")+ylab("Increase in participation")+ggtitle(" ")


library(cowplot) 
prow_lr<-plot_grid(ggr0913_2,ggr0913_1,ggr0711_2,ggr0711_1, ncol=2)
prow_lr
